//
//  EnvironmentDetailView.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/6.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "EnvironmentDetailView.h"
#import "EnvironmentDetailCell.h"
#import "PollingTableModel.h"
@interface EnvironmentDetailView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSString *Uuid;
@property(nonatomic,strong)NSMutableArray *dataAr;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic)NSInteger index;

@end
@implementation EnvironmentDetailView

-(instancetype)initWithFrame:(CGRect)frame withUuid:(NSString*)Uuid WithItemUuid:(NSString*)itemUuid withIndex:(NSInteger)index{
    self = [super initWithFrame:frame];
    if (self) {
        self.Uuid=Uuid;
        self.index=index;
        self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, self.bounds.size.height) style:UITableViewStylePlain];
        self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.tableView.showsVerticalScrollIndicator =NO;
        [self addSubview:self.tableView];
        self.tableView.delegate=self;
        self.tableView.dataSource=self;
        self.tableView.backgroundColor =BackColor;

        
        [self.tableView registerClass:[EnvironmentDetailCell class] forCellReuseIdentifier:@"cell"];

        [CClass HttpRequestTask:YES withUrl:@"inspection_getSubReport" withData:@{@"uuid":self.Uuid,@"item_uuid":itemUuid} notifyError:YES onSuccess:^(BaseModel *model) {
            self.dataAr=[model.B_data objectForKey:@"list"];
            [self.tableView reloadData];

        } onError:^(BaseModel *model) {
            
        }];
        self.tableView.tableFooterView=[self footTableView];

        
    }
    
    return self;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    EnvironmentDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[EnvironmentDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    NSMutableDictionary *dic=[self.dataAr objectAtIndex:indexPath.row];
    cell.line.backgroundColor=LineColor;
    
    cell.nameLab.text =[dic objectForKey:@"name"];
    NSString *state;
    if ([F_NSpace([dic objectForKey:@"result"]) isEqualToString:@""]) {
        state=@"";
    }else
    {
        // result字段： 1 - 正常; 2 - 异常
        if ([[dic objectForKey:@"result"] integerValue] ==1) {
            state=[dic objectForKey:@"good_text"];
            cell.contexLab.textColor =C(@"b9b8b8");

            
        }else if([[dic objectForKey:@"result"] integerValue] ==2)
        {
            state=[dic objectForKey:@"bad_text"];
            cell.contexLab.textColor =MainColor;
            
            
        }else
        {
            state=@"";

        }
        
    }
    NSString *remark;
    if ([F_NSpace([dic objectForKey:@"content"]) isEqualToString:@""])
    {
            
        remark=@"";
        
    }else
    {
        remark=[NSString stringWithFormat:@"；%@", [dic objectForKey:@"content"]];
        
    }
    cell.contexLab.text =[NSString stringWithFormat:@"%@ %@",state,remark];

    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dic=[self.dataAr objectAtIndex:indexPath.row];
    CGFloat height;
    NSString *remark;
    if ([F_NSpace([dic objectForKey:@"content"]) isEqualToString:@""])
    {
        
        remark=@"";
        
    }else
    {
        remark=[NSString stringWithFormat:@"；%@", [dic objectForKey:@"content"]];
        
    }
    NSString *context =[NSString stringWithFormat:@"正常 %@",remark];
    if (context.length>10) {
        CGSize  textDescridSize = [context sizeWithFont:F(12) constrainedToSize:CGSizeMake((WindowWidth-30)/2-5, 900)];
        height=textDescridSize.height+40;
    }else
    {
        height=50;
    }
    
    
    return height;
}
- (UIView*)footTableView
{    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 90)];
    view.backgroundColor =BackColor;
    
    UIButton*upBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:upBtn];
    CGFloat widtnBtn=(WindowWidth-60-40)/2;
    upBtn.frame=CGRectMake(30, 25, widtnBtn, 30);
    [upBtn setBackgroundColor:MainColor];
    upBtn.layer.masksToBounds=YES;
    upBtn.layer.cornerRadius=6;
    upBtn.titleLabel.font=F(16);
    [upBtn setTitle:@"上一页" forState:UIControlStateNormal];
    [upBtn bk_addEventHandler:^(id sender) {
        
        if (self.gotoUpward) {
            self.gotoUpward(self.Uuid);
        }
        
    } forControlEvents:UIControlEventTouchUpInside];
    UIButton*downBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:downBtn];
    downBtn.frame=CGRectMake(WindowWidth-30-widtnBtn, 25, widtnBtn, 30);
    [downBtn setBackgroundColor:MainColor];
    downBtn.layer.masksToBounds=YES;
    downBtn.layer.cornerRadius=6;
    downBtn.titleLabel.font=F(16);
    [downBtn setTitle:@"下一页" forState:UIControlStateNormal];
    [downBtn bk_addEventHandler:^(id sender) {
        
        if (self.gotoDownward) {
            self.gotoDownward(self.Uuid);
        }
        
    } forControlEvents:UIControlEventTouchUpInside];
    if (_index==1) {
        
        upBtn.hidden=YES;
        downBtn.frame=CGRectMake(30, 25, WindowWidth-60, 30);
        
    }else
    {
        upBtn.hidden=NO;
        upBtn.frame=CGRectMake(30, 25, widtnBtn, 30);
        downBtn.frame=CGRectMake(WindowWidth-30-widtnBtn, 25, widtnBtn, 30);
        
    }
    return view;
}
@end
